//Demo
layui.use('form', function(){
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function(data){
        layer.msg(JSON.stringify(data.field));
        return false;
    });
});

//表单
layui.use(['table'], function(){
    var table = layui.table;
    table.render({
        elem: '#members'
        ,size: 'lg' //开启大尺寸样式
        ,url:'/admin/users/showUsers/'
        ,cols: [[
            {type:'checkbox'}
            // {field: 'id', title: 'id', sort: true, fixed: 'left'}
            ,{field: 'username', title: '用户名',width:100}
            ,{field: 'nickname', title: '昵称',width:100}
            ,{field: 'createTime', title: '创建时间'}
            ,{title: "操作", fixed: 'right', align: 'center', toolbar: '#bar'}
        ]]
        ,page: true
        ,id: 'select'
    });
    //模糊查询
    $("#select").click(function () {
        var username = $("#username").val()
        var formData = {
            userName:username
            ,page:1
            ,limit:10
        }
        $.ajax({
            url:"/admin/users/showUsers"
            ,method:"get"
            ,data:formData
            ,success: (response) => {
                table.reload('select',{
                    where: {
                        userName:username
                    }
                    ,page:{
                        curr:1
                    }
                })
            },
        })
    });
    table.on('tool(members)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
        var id = data.stringId;
        if (layEvent == "select") {//查看
            $.ajax({
                url:'/admin/users/selectUserIb'
                ,method: 'get'
                ,data:{id:id}
                ,success: (response) => {
                    console.log(response)
                    $("#uname").val(response.username);
                    $("#nname").val(response.nickname);
                    var members_open = layer.open({
                        type: 1,
                        area: ['400px', '400px'],
                        title: "查看会员"
                        ,content: $("#sel_members")
                        ,shade: 0
                        ,btn: []
                        ,cancel: function (layero, index) {//关闭按钮
                            layer.close(members_open);
                        },
                    })
                }
            });
        }
        else if(layEvent == "update"){
            $.ajax({
                url:'/admin/users/selectUserIb'
                ,method: 'get'
                ,data:{id:id}
                ,success: (response) => {
                    console.log(response)
                    $("#up_uname").val(response.username);
                    $("#up_nname").val(response.nickname);
                    $("#up_password").val(response.password)
                    var user_open = layer.open({
                        type: 1,
                        area: ['400px', '300px'],
                        title: "会员修改"
                        ,content: $("#upd_members")
                        ,shade: 0
                        ,btn: ["修改","取消"]
                        ,btn1:function(index,layero){
                            var username = $("#up_uname").val()
                            var nickname = $("#up_nname").val()
                            var id = response.id
                            var password = $("#up_password").val()
                            var formData = {
                                username:username
                                ,nickname:nickname
                                ,id:id
                                ,password:password
                            }
                            $.ajax({
                                url:"/admin/users/updateUser"
                                ,method : "post"
                                ,data:formData
                                ,success: (response)=>{
                                    if (response.code == 200){
                                        alert("修改成功")
                                        location.reload()
                                    }else{
                                        alert(response.msg)
                                    }
                                }
                            });
                        }
                        ,cancel: function (layero, index) {//关闭按钮
                            layer.close(user_open);

                        },
                    })
                }
            });
        }
        else if (layEvent == "delete"){
            layer.confirm('你确认要删除此会员么', {
                time: 0,
                title: '删除会员',
                btn: ['确认', '取消'],
            }, function (index) {
                $.ajax({
                    url:"/admin/users/deleteUserById"
                    ,method : "get"
                    ,data:{'id':id}
                    ,success: (response)=>{
                        if (response.code == 200){
                            alert("删除成功")
                            location.reload()
                        }else{
                            alert(response.msg)
                        }
                    }
                });
            },function (index) {
                layer.close(index);
            });
        }
    });
});

//添加会员
$(function () {
    $("#add").click(function () {
        var add_tags = layer.open({
            type:1,
            area:['350px','350px'],
            title: "新增会员"
            ,content: $("#add_members"),
            shade: 0,
            btn: ['提交', '重置']
            ,btn1: function(index, layero){//提交按钮
                var username = $("#add_username").val()
                var password = $("#add_password").val()
                var nickname = $("#add_nickname").val()

                if(username == null || username == ""){
                    alert("用户名不能为空")
                    return false;
                }if(password == null || password == ""){
                    alert("密码不能为空")
                    return false;
                }if(nickname == null || nickname == ""){
                    alert("昵称不能为空")
                    return false;
                }

                var formData = {
                    username: username,
                    password: password,
                    nickname: nickname
                }
                $.ajax({
                    url: '/admin/users/addUser',
                    method: "post",
                    data: formData,
                    success: (response) => {
                        if (response.code == 200) {
                            alert("创建用户成功")
                            location.reload()
                        } else {
                            alert(response.msg)
                            return false
                        }
                    },
                })
            },
            btn2: function(index, layero){//重置按钮
                $("#username").val("")
                $("#password").val("")
                $("#nickname").val("")
                return false
            },
            cancel: function(layero,index){//关闭按钮
                layer.close(add_tags);
            }
        });
    })

})

//批量删除
$(function(){
    $("#dels").click(function () {
        var getids = "";
        var id_data = layui.table.checkStatus('select').data
        for (var i = 0; i < id_data.length; i+=1){
            getids += id_data[i]['stringId']+",";
        }
        if (id_data.length == 0){
            alert("请选择要删除的信息")
        }else{
            layer.confirm('你确认要删除这些角色么', {
                time: 0,
                title: '批量角色',
                btn: ['确认', '取消'],
            }, function (index) {
                $.ajax({
                    url:"/admin/users/deleteUsersByIds"
                    ,method : "get"
                    ,data:{'ids':getids}
                    ,success: (response)=>{
                        if (response.code == 200){
                            alert("删除成功")
                            location.reload();
                        }else{
                            alert(response.msg)
                        }
                    }
                });
            },function (index) {
                layer.close(index);
            });
        }
    })
})